package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.mjdb.data.field.NullField;
import org.one.stone.soup.xml.XmlElement;
 /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataBehaviour extends NullField implements ITag {
	public static String DEFAULT_ID = "_Behaviour";

		DataState draggable = new DataState(false);
		DataState selectable = new DataState(false);
		DataState rotatable = new DataState(false);
		DataState dragging = new DataState(false);
		DataState rotating = new DataState(false);
		DataState over = new DataState(false);
		DataState selected = new DataState(false);
		DataState dropped = new DataState(false);
		DataState toggled = new DataState(false);
		DataPoint velocity = new DataPoint();
public DataBehaviour() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{
	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.DRAGGABLE))
		{
			draggable = new DataState(false);
	  		draggable.buildFromgrfxML(element);
	  	}
		else if(eleName.equals(Token.SELECTABLE))
		{
			selectable = new DataState(false);
			selectable.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.ROTATABLE))
		{
			rotatable = new DataState(false);
			rotatable.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.DRAGGING))
		{
			dragging = new DataState(false);
			dragging.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.ROTATING))
		{
			rotating = new DataState(false);
	 		rotating.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.OVER))
		{
			over = new DataState(false);
	  		over.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.SELECTED))
		{
			selected = new DataState(false);
			selected.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.DROPPED))
		{
			dropped = new DataState(false);
			dropped.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.TOGGLED))
		{
			toggled = new DataState(false);
			toggled.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.VELOCITY))
		{
			velocity = new DataPoint(TagModifier.POINT_TYPE_VECTOR);
	  		velocity.buildFromgrfxML(element,TagModifier.POINT_TYPE_VECTOR);
		}
		else
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.BEHAVIOUR,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.DRAGGABLE))
		{
	  		draggable.copyFromgrfxML(element);
	  	}
		else if(eleName.equals(Token.SELECTABLE))
		{
			selectable.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.ROTATABLE))
		{
			rotatable.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.DRAGGING))
		{
			dragging.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.ROTATING))
		{
	 		rotating.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.OVER))
		{
	  		over.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.SELECTED))
		{
			selected.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.DROPPED))
		{
			dropped.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.TOGGLED))
		{
			toggled.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.VELOCITY))
		{
	  		velocity.copyFromgrfxML(element,TagModifier.POINT_TYPE_VECTOR);
		}
		else
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.BEHAVIOUR,""+element.getStartLineNo()} );
		}
	}
}
public DataBehaviour getClone() {

	DataBehaviour newBehaviour = new DataBehaviour();

	newBehaviour.draggable = draggable.getClone();
	newBehaviour.dragging = dragging.getClone();
	newBehaviour.over = over.getClone();
	newBehaviour.rotatable = rotatable.getClone();
	newBehaviour.rotating = rotating.getClone();
	newBehaviour.selectable = selectable.getClone();
	newBehaviour.selected = selected.getClone();
	newBehaviour.dropped = dropped.getClone();
	newBehaviour.toggled = toggled.getClone();
	newBehaviour.velocity = velocity.getClone();

	return newBehaviour;
}
public DataBehaviour getClone(String path) {

	DataBehaviour newBehaviour = new DataBehaviour();

	GrfxMLStoreProcessor.setCloneName( path,this,newBehaviour );

	newBehaviour.draggable = draggable.getClone(path);
	newBehaviour.dragging = dragging.getClone(path);
	newBehaviour.over = over.getClone(path);
	newBehaviour.rotatable = rotatable.getClone(path);
	newBehaviour.rotating = rotating.getClone(path);
	newBehaviour.selectable = selectable.getClone(path);
	newBehaviour.selected = selected.getClone(path);
	newBehaviour.dropped = dropped.getClone(path);
	newBehaviour.toggled = toggled.getClone(path);
	newBehaviour.velocity = velocity.getClone(path);

	return newBehaviour;
}
public DataState getDraggable() {
	return draggable;
}
public DataState getDragging() {
	return dragging;
}
/**
 * Insert the method's description here.
 * Creation date: (31/10/03 17:35:24)
 * @return org.one.stone.soup.grfxML.DataState
 */
public DataState getDropped() {
	return dropped;
}
public DataState getOver() {
	return over;
}
public DataState getRotatable() {
	return rotatable;
}
public DataState getRotating() {
	return rotating;
}
public DataState getSelectable() {
	return selectable;
}
public DataState getSelected() {
	return selected;
}
public DataState getToggled() {
	return toggled;
}
public DataPoint getVelocity() {
	return velocity;
}
public void register(DataLinker store) {

	try{
		store.add(draggable,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(selectable,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(rotatable,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(dragging,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(rotating,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(over,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(selected,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(dropped,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(toggled,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(velocity,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj, newObj, false);
}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	if(draggable==oldObj)
		draggable=(DataState)newObj;

	if(selectable==oldObj)
		selectable=(DataState)newObj;

	if(rotatable==oldObj)
		rotatable=(DataState)newObj;

	if(dragging==oldObj)
		dragging=(DataState)newObj;

	if(rotating==oldObj)
		rotating=(DataState)newObj;

	if(over==oldObj)
		over=(DataState)newObj;

	if(selected==oldObj)
		selected=(DataState)newObj;

	if(dropped==oldObj)
		dropped=(DataState)newObj;

	if(toggled==oldObj)
		toggled=(DataState)newObj;

	if(velocity==oldObj)
		velocity=(DataPoint)newObj;
}
public void resetChanged() {
	draggable.resetChanged();
	selectable.resetChanged();
	rotatable.resetChanged();
	dragging.resetChanged();
	rotating.resetChanged();
	over.resetChanged();
	selected.resetChanged();
	toggled.resetChanged();
	velocity.resetChanged();
}
public void setDraggable(DataState newDraggable) {
	draggable = newDraggable;
}
public void setDragging(DataState newDragging) {
	dragging = newDragging;
}
/**
 * Insert the method's description here.
 * Creation date: (31/10/03 17:35:24)
 * @param newDropped org.one.stone.soup.grfxML.DataState
 */
public void setDropped(DataState newDropped) {
	dropped = newDropped;
}
public void setOver(DataState newOver) {
	over = newOver;
}
public void setRotatable(DataState newRotatable) {
	rotatable = newRotatable;
}
public void setRotating(DataState newRotating) {
	rotating = newRotating;
}
public void setSelectable(DataState newSelectable) {
	selectable = newSelectable;
}
public void setSelected(DataState newSelected) {
	selected = newSelected;
}
public void setToggled(DataState newToggled) {
	toggled = newToggled;
}
public void setVelocity(DataPoint newVelocity) {
	velocity = newVelocity;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += draggable.togrfxML(Token.DRAGGABLE);
	xml += selectable.togrfxML(Token.SELECTABLE);
	xml += rotatable.togrfxML(Token.ROTATABLE);
	xml += dragging.togrfxML(Token.DRAGGING);
	xml += rotating.togrfxML(Token.ROTATING);
	xml += over.togrfxML(Token.OVER);
	xml += selected.togrfxML(Token.SELECTED);
	xml += toggled.togrfxML(Token.TOGGLED);
	xml += velocity.togrfxML(Token.VELOCITY);
	xml += "</" + name + ">\n";
	return xml;
}
public String togrfxML(String name,TagModifier modifier) {
	return togrfxML(name);
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
}
